<?php
class Ajax_m extends CI_Model{

	/* -------------------------------- users ---------------------------- */
	/* tampil users */
	function tampil()
	{
		$a="select * from t_m_user order by id_user DESC";
		return $this->db->query($a)->result();
	}
	
	/* create users */
	function create_users($data)
	{
		$this->db->insert('t_m_user', $data);
		return;
	}
	
	/* update users */
	function update_users($id_user)
	{
		$b="select * from t_m_user where id_user='$id_user'";
		return $this->db->query($b)->result();
	}
	
	/* proses update users */
	function proses_update_users($id_user)
	{
		$pass=$this->input->post('password');
		if ($pass==''){
			$data=array(
				'nama_lengkap' => $this->input->post('nama_lengkap'),
				'username' => $this->input->post('username'),
				'no_telp' => $this->input->post('no_telp'),
				'email' => $this->input->post('email'),
				'level' => $this->input->post('level')
			);
		}else{
			$pass=md5($this->input->post('password'));
			$data=array(
				'nama_lengkap' => $this->input->post('nama_lengkap'),
				'username' => $this->input->post('username'),
				'password'=> $pass,
				'no_telp' => $this->input->post('no_telp'),
				'email' => $this->input->post('email'),
				'level' => $this->input->post('level')
			);
		}
		$this->db->where('id_user', $id_user);
        $this->db->update('t_m_user', $data);
	}
	
	/* delete users */
	function delete_users()
	{
		$this->db->where('id_user', $this->uri->segment(3));
		$this->db->delete('t_m_user');
	}

	
	
	/* -------------------------------- departemen ---------------------------- */
	/* tampil departemen */
	function tampil_departemen()
	{
		$a="select * from t_m_departemen order by id_dept DESC";
		return $this->db->query($a)->result();
	}
	
	/* create departemen */
	function create_departemen($data)
	{
		$this->db->insert('t_m_departemen', $data);
		return;
	}
	
	/* update departemen */
	function update_departemen($id_dept)
	{
		$c="select * from t_m_departemen where id_dept='$id_dept'";
		return $this->db->query($c)->result();
	}
	
	/* proses update departemen */
	function proses_update_departemen($id_dept)
	{
		$data=array(
			'nm_dept' => $this->input->post('nm_dept')
		);
		$this->db->where('id_dept', $id_dept);
        $this->db->update('t_m_departemen', $data);
	}
	
	/* delete departemen */
	function delete_departemen()
	{
		$this->db->where('id_dept', $this->uri->segment(3));
		$this->db->delete('t_m_departemen');
	}
	
	
	
	/* -------------------------------- jam kerja ---------------------------- */
	/* tampil jam kerja */
	function tampil_jam_kerja()
	{
		$a="select * from t_m_jamkerja order by id DESC";
		return $this->db->query($a)->result();
	}
	
	/* create jam kerja */
	function create_jam_kerja($data)
	{
		$this->db->insert('t_m_jamkerja', $data);
		return;
	}
	
	/* update departemen */
	function update_jam_kerja($id)
	{
		$d="select * from t_m_jamkerja where id='$id'";
		return $this->db->query($d)->result();
	}
	
	/* proses update departemen */
	function proses_update_jam_kerja($id)
	{
		$data=array(
			'jam_masuk' => $this->input->post('jam_masuk'),
			'jam_keluar' => $this->input->post('jam_keluar'),
			'b_active' => $this->input->post('b_aktive')
		);
		$this->db->where('id', $id);
        $this->db->update('t_m_jamkerja', $data);
	}
	
	/* delete jam kerja*/
	function delete_jam_kerja()
	{
		$this->db->where('id', $this->uri->segment(3));
		$this->db->delete('t_m_jamkerja');
	}
	
	
	
	/* -------------------------------- jam karyawan ---------------------------- */
	/* tampil karyawan */
	function tampil_karyawan()
	{
		$a="select * from t_m_karyawan, t_m_departemen where t_m_karyawan.id_dept=t_m_departemen.id_dept order by NIP DESC";
		return $this->db->query($a)->result();
	}
	
	/* create karyawan */
	function create_karyawan($data)
	{
		$this->db->insert('t_m_karyawan', $data);
		return;
	}
	
	/* update karyawan */
	function update_karyawan($NIP)
	{
		$e="select * from t_m_karyawan where NIP='$NIP'";
		return $this->db->query($e)->result();
	}
	
	/* delete karyawan*/
	function delete_karyawan()
	{
		$this->db->where('NIP', $this->uri->segment(3));
		$this->db->delete('t_m_karyawan');
	}
	
	
	
	/* -------------------------------- jam absensi ---------------------------- */
	/* tampil absensi */
	function tampil_absensi()
	{
		$a="select * from t_d_absensi, t_m_karyawan , t_m_departemen
			where 
			t_d_absensi.NIP=t_m_karyawan.NIP 
			AND
			t_m_karyawan.id_dept=t_m_departemen.id_dept and date(masuk)=curdate()
			order by id DESC";
		return $this->db->query($a)->result();
	}
}
?>